define(['jquery','fullcalendar','fullcalendar_zh-cn','formServ','init','bsMsgServ','deptUserSelect'
			,'moment','bootstrap-datetimepicker','bootstrap-datetimepicker.zh-CN'],
		function($,fullcalendar,lang,formServ,init,bsMsgServ,deptUserSelect
					,moment,datetimepicker,datetimepickerLang){
		
		var qryUrl = '/'+init.getSite()+'/oaConferenceroomApply2/query/getRoomList.action';
		
		//页面根节点
		var rootid = 'meeting-main';
		//会议室基本信息
		var roomConfig = {} ;
		var funcDef = {
			initUser:function(){
			
			var _usr =init.getCurrentUser();
				var _el = $('#'+rootid +' #apply_name')
				if(_el){
					_el.val(_usr.name)
					
				}
				
				_el = $('#'+rootid +' #apply_id')
				if(_el){
					_el.val(_usr.id)
				}
				
				//初始化部门				
				_el = $('#'+rootid +' #apply_deptname')
				if(_el){
					_el.val(_usr.deptName)
					
				}
				
				_el = $('#'+rootid +' #apply_deptid')
				if(_el){
					_el.val(_usr.deptid)
				}
			},
			/**
			 * 初始化会议室列表
			 */
			initRoomList:function(){
				/**********会议室选择列表*****/
				
				
				var _param = {
					async: false,
					url:'/'+init.getSite() + '/conferenceroomBaseinfo/getList.action',
					success:function(data){
						if(data && data.length>0){
							
							var _html = '';
							$.each(data,function(i,o){
								if(o && o.id){
									//缓存会议室信息
									roomConfig[o.id]=o;
									
									//生成下拉项
									_html +='<option value="' +o.id +'">' + o.room_name + '</option>';
								}
							})
							
							var _el = $('#'+rootid + ' #room_id');
							if(_el.length>0 && _html){
								_el.append(_html)	
							}
							
							_el.unbind('change');
							_el.bind('change',function(data){
								var _id = _el.val();
								if(_id){
									var _room = roomConfig[_id];
									if(_room){
										$('#'+rootid + ' #seat_number').val(_room.seat_number);
										$('#'+rootid + ' #room_address').val(_room.room_address);
										$('#'+rootid + ' #seat_name').val(_room.room_name);
									}
								}
							})
						}
					}
					,
					failure:function(){
						bsMsgServ.errorSlide('数据加载失败.')
					}
				}
				
				$.ajax(_param);
			}
			,
			initDatepicker:function(){
				
				var _start = $('#'+rootid + ' #start_room_time')
				var _end = $('#'+rootid + ' #end_room_time')
				
				var _param ={
					language:  'zh-CN',
					format: "yyyy-mm-dd hh:ii:ss",
					autoclose: true,
					minView: "month",
					maxView: "decade",
					todayBtn: true,
					pickerPosition: "bottom-left"
				}
				
				if(_start.length==1){
					if(window.console){
						console.log( ' start length: ' + 1)
					}
					_start.datetimepicker(_param);
					
					if(_end.length==1){
						if(window.console){
							console.log( ' start length: ' + 1)
						}
						_start.on("click",function(ev){
							_start.datetimepicker("setEndDate", _end.val());
						})
						
						_end.datetimepicker(_param)
							.on("click", function (ev) {
								_end.datetimepicker("setStartDate", _start.val());
							})
					}
				}				
			}
			,
				
			refreshList:function(_options){
				$('#'+rootid +' #calendar-root').fullCalendar( 'refetchEvents')
			},
			refreshForm:function(data){
				var _form = $('#'+rootid +' .main-form')
				if(_form.length>0){
					_form.form('clear');
					_form.form('load',data)
				}
			},
			btnClick:function(e){
				
				init.applyBtnEvt({btnEvt:e,btnConfig:btnEvent});
			}
			,
			
			
			getFormRule:function(){
				var rules = {
					errorElement : 'span',  
		            errorClass : 'js-required-field',  
		            focusInvalid : true,
					rules:{
						apply_deptname: 'required',
						apply_name: 'required',
						start_room_time: 'required',
						end_room_time: 'required',
						name:'required',
						seat_number:'required',
						room_address:'required'
						
						
					}
					,
					messages:{
						apply_deptname: '部门不允许为空',
						apply_name: '人员不允许为空',
						start_room_time: '开始时间不允许为空',
						end_room_time: '结束时间不允许为空',
						name: '会议主题不能为空',
						seat_number:'座位数量不能为空',
						room_address:'会议室地点不能为空'
						
					}
				}
				
				return rules;
			}
		}

		var btnEvent = {
			add:function(e){
				var _form = $('#'+rootid +' .main-form');
				_form.form('clear');
				funcDef.initUser();
			}
			,
			save:function(e){
				btnEvent.flow({
					url:'/oaConferenceroomApply/flowSave.action',
					evt:e
				})
			}
			,
			send:function(e){
				btnEvent.flow({
					url:'/oaConferenceroomApply/flowTaskComplete.action',
					evt:e
				})
			}
			,
			//删除
			del:function(evt){
			
				//获取form数据
				var _f = $('#'+rootid +' .main-form')
				
				var _data = formServ.getDataObj({form:_f});
				
				//判断记录id是否存在
				if(_data && _data.id){
					var _usr = init.getCurrentUser();
					//判断当前用户是否与创建人相同
					if(_data.apply_id && (_data.apply_id == _usr.id)){
						//相同，弹窗提示删除
						$.messager.confirm('警告', '确定执行删除操作?', function(r){						
							if( r){
								var _param = {
									url :'/'+init.getSite()+'/oaConferenceroomApply/flowDel.action',
									dataType : 'json',
									data : {id:_data.id},									
									contentType : 'application/x-www-form-urlencoded; charset=UTF-8',
									success:function(){
										funcDef.refreshList();
										bsMsgServ.msgSlide('操作成功！')
										$(evt.currentTarget).removeClass('disabled');
									},
									error:function(){
										bsMsgServ.errorSlide('操作失败！')
										$(evt.currentTarget).removeClass('disabled');
									}
								}
								$.ajax(_param)
							}
						})
					}else{
					//不相同，提示用户后退出
						bsMsgServ.errorSlide('删除失败！只允许删除当前用户创建记录.');
						$(evt.currentTarget).removeClass('disabled');
					}
				}else{
					bsMsgServ.errorSlide('请选择要删除的记录！');
					$(evt.currentTarget).removeClass('disabled');
				}
				
			},
			
			/**
			 * 流程执行方法
			 * @param _options.url	流程url
			 * @param _options.evt	事件
			 */
			flow:function(_options){
				//获取form数据
				var _f = $('#'+rootid +' .main-form')
				
				//获取事件
				var evt = _options.evt;
				
				
				//判断form必填项
				if(
					!formServ.validateForm({
						form:_f,
						rules:funcDef.getFormRule()
					})
				){
					bsMsgServ.errorSlide('请填写表单内容');
					//释放按钮禁用状态
					$(evt.currentTarget).removeClass('disabled');
					return
				}
				
				var _data = formServ.getDataObj({form:_f});

				//流程定义信息
				var _applyCoduConf = init.getBizConfig({bizcode:'oaConferenceroomApply'});
				if(_applyCoduConf && _applyCoduConf.flowid){
					_data.flowid = _applyCoduConf.flowid;
					_data.fullBizcode = _applyCoduConf.fullBizcode;
				}else{
					
					bsMsgServ.errorSlide('流程定义错误！')
					$(evt.currentTarget).removeClass('disabled');
					return;
				}
				
				var _param = {
					url :'/'+init.getSite()+_options.url,
					dataType : 'json',
					data : $.toJSON(_data),
					type : 'post',
					contentType : "appliction/json;charset=utf-8",
					success:function(){
						funcDef.refreshList();
						bsMsgServ.msgSlide('操作成功！')
						$(evt.currentTarget).removeClass('disabled');
					},
					error:function(){
						bsMsgServ.errorSlide('操作失败！')
						$(evt.currentTarget).removeClass('disabled');
					}
				}
				$.ajax(_param)
				
				_f.form('clear')
			}
		}
		var public_method = {
			
			setRootid:function(_options){
				if(_options && _options.rootid){
					rootid = _options.rootid
				}else{
					rootid = 'meeting-main';
				}
				
			}
			,
			/**
			 * 初始化页面
			 * @param _options.rootid	根节点id
			 */
			initPage:function(_options){
				
				public_method.setRootid(_options);
				$('#'+rootid +' #calendar-root').fullCalendar({
					header: {
						left: 'prev,next today',
						center: 'title',
						right: 'month,agendaWeek,agendaDay'
					},
					editable: false,
					eventLimit: true, // allow "more" link when too many events
					lang:'zh-cn',
				    
				    
				    theme:false,
				    events:qryUrl,
			        eventClick: function(events, jsEvent, view) {
			        	if(events && events._id){
				        	var _param = {
								url :'/'+init.getSite()+'/oaConferenceroomApply2/query/getOne.action?id='+events._id,					
								type : 'post',
								contentType : "appliction/json;charset=utf-8",
								success:funcDef.refreshForm,
								error:function(){
									bsMsgServ.errorSlide('操作失败！')
								}
							}
							$.ajax(_param)
						}			        	
					}				    
				});		
				
		
				/*****绑定按钮函数*******/
				
				var $btn= $("#" +rootid +" button.btn")
			    
			    if(window.console){
					console.log('length:'+$btn.length)
				}
				$btn.unbind('click');
				$btn.bind('click',funcDef.btnClick)	
				
				/******弹窗选择人员及部门********/
				var el = $("#" +rootid +" #apply_name")
				console.log('el.length:' + el.length)
//				$(el).textbox({
//					onClickButton:function(data){								
//						deptUserSelect.openPage({
//							userids:rootid +' #apply_id',
//							usernames:rootid +' #apply_name',
//							deptid:rootid +' #apply_deptid',
//							deptname:rootid +' #apply_deptname'					
//						})								
//					}
//				})
				
				//初始化会议室列表
				funcDef.initRoomList();
				
				//初始化用户
				funcDef.initUser();
				funcDef.initDatepicker();
//				$.messager.progress('close');	
			}
			,
			/**
			 * 初始化组件
			 * @param _options.rootid 	页面根节点id
			 */
			initElement:function(_options){
				
				
				
				//初始化用户
				funcDef.initUser();
				funcDef.initDatepicker();
			}
			,
			beforeInit:function(_options){
				//设置页面根节点
				public_method.setRootid(_options);
				//初始化会议室列表
				funcDef.initRoomList();
			}
			
		}
		return public_method;
	});